package com.example.mediademo.decode;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.util.Log;

import com.example.mediademo.decode.extractor.AudioExtractor;
import com.example.mediademo.decode.interfaces.IDecoderProgress;
import com.example.mediademo.decode.interfaces.IExtractor;

import java.lang.reflect.Array;
import java.nio.ByteBuffer;

public class AudioDecoder extends BaseDecoder {

    /** 采样率 */
    private int mSampleRate = -1;
    /** 音频通道数 */
    private int mChannels = 1;
    /** PCM采样位数 */
    private int mEncodeBit = AudioFormat.ENCODING_PCM_16BIT;
    /** 音频播放器 */
    private AudioTrack mAudioTrack = null;
    private short[] mAudioOutTempBuffer = null;


    public AudioDecoder(String filePath) {
        super(filePath);
    }


    @Override
    protected boolean check() {
        return true;
    }

    @Override
    protected void initSpecParams(MediaFormat format) {
        mChannels = format.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
        mSampleRate = format.getInteger(MediaFormat.KEY_SAMPLE_RATE);
        if (format.containsKey(MediaFormat.KEY_PCM_ENCODING)) {
            mEncodeBit = format.getInteger(MediaFormat.KEY_PCM_ENCODING);
        } else {
            mEncodeBit = AudioFormat.ENCODING_PCM_16BIT;
        }
    }

    @Override
    protected IExtractor initExtractor() {
        return new AudioExtractor(mFilePath);
    }

    @Override
    protected boolean initRender() {
        int channel;
        if (mChannels == 1) {
            // 单声道
            channel = AudioFormat.CHANNEL_OUT_MONO;
        } else {
            // 双声道
            channel = AudioFormat.CHANNEL_OUT_STEREO;
        }
        // 获取最小缓冲区
        int minBufferSize = AudioTrack.getMinBufferSize(mSampleRate,channel,mEncodeBit);
        mAudioOutTempBuffer = new short[minBufferSize / 2];
        mAudioTrack = new AudioTrack(
                AudioManager.STREAM_MUSIC,// 播放类型，音乐
                mSampleRate,// 采样率
                channel,    // 通道
                mEncodeBit, // 采样位数
                minBufferSize,  // 缓冲区大小
                AudioTrack.MODE_STREAM);//播放模式:数据流动态写入，另一种是一次性写入
        if (mAudioTrack != null) {
            mAudioTrack.play();
        }
        return true;
    }

    @Override
    protected boolean configCodec(MediaCodec mCodec, MediaFormat format) {
        mCodec.configure(format,null,null,0);
        return true;
    }

    @Override
    protected void render(ByteBuffer outputBuffer, MediaCodec.BufferInfo bufferInfo) {
        Log.d("BaseDecoder","Audio 渲染");
        if (mAudioOutTempBuffer != null && mAudioOutTempBuffer.length < bufferInfo.size / 2) {
            mAudioOutTempBuffer = new short[bufferInfo.size / 2];
        }
        outputBuffer.position(0);
        outputBuffer.asShortBuffer().get(mAudioOutTempBuffer,0,bufferInfo.size / 2);
        if (mAudioTrack != null) {
            mAudioTrack.write(mAudioOutTempBuffer,0,bufferInfo.size / 2);
        }
    }

    @Override
    protected void doneDecode() {
        if (mAudioTrack != null) {
            mAudioTrack.stop();
            mAudioTrack.release();
        }
    }

    @Override
    public void setSizeListener(IDecoderProgress listener) {

    }
}
